package com.md.datastore;

import android.text.TextUtils;
import android.util.Log;

import com.md.basedpc.persistence.PHDbUtils;
import com.md.network.api.Album;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyue
 * create 2021/4/4
 * desc 管理收藏专辑DB 参照DownLoadDBManager
 */
public class MysaveAlbumManager {
    public static MysaveAlbumManager getIns() {
        return InsHolder.INS;
    }

    private MysaveAlbumManager() {

    }

    static class InsHolder {
        private static MysaveAlbumManager INS = new MysaveAlbumManager();
    }

    public boolean saveAlbum(Album album) {
        boolean isSuc = false;
        if (album != null) {
            MysaveAlbumDB db = new MysaveAlbumDB();
            db.setAuthor(album.getAuthor());
            db.setCover(album.getCoverUrl());
            db.setId(album.getId());
            db.setDesc(album.getDesc());
            db.setName(album.getName());
            if (album.getAudios() !=null){
                db.setAudioNum(String.valueOf(album.getAudios().size()));
            }
            isSuc = saveAlbum(db);
        }
        return isSuc;
    }


    public MysaveAlbumDB getAlbumDBById(String albumId) {
        MysaveAlbumDB res = null;
        String where = " 1=1 ";
        where += " and id=" + albumId;
        try {
            List<MysaveAlbumDB> list = PHDbUtils.getInstance().query(MysaveAlbumDB.class, where);
            if (list != null) {
                res = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }


    public boolean update(MysaveAlbumDB album) {
        boolean isSuc = false;
        try {
            isSuc = PHDbUtils.getInstance().update(album);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isSuc;
    }

    /**
     * 保存 专辑信息
     *
     * @param albumDB
     */
    private boolean saveAlbum(MysaveAlbumDB albumDB) {
        boolean isSaved = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumDB.getId()))
            where += " and id=" + albumDB.getId() + "";
        try {
            List<MysaveAlbumDB> dbs = PHDbUtils.getInstance().query(MysaveAlbumDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                isSaved = PHDbUtils.getInstance().insert(albumDB);
            } else {
                return true;
            }
        } catch (Exception e) {
            Log.e("SelfAlbumManager", e.toString());
        }
        return isSaved;
    }


    public boolean deleteAlbumById(String albumId) {
        boolean isDelSuccess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumId)) {
            where += " and id=" + albumId + "";
            try {
                isDelSuccess = PHDbUtils.getInstance().delete(MysaveAlbumDB.class, where);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isDelSuccess;
    }

    /**
     * 获取所有的自建专辑
     */
    public List<Album> getAllSaveAlbum() {
        List<Album> res = new ArrayList<>();
        String where = " 1=1 ";
        try {
            List<MysaveAlbumDB> dbList = PHDbUtils.getInstance().query(MysaveAlbumDB.class, where);
            if (dbList != null && dbList.size() > 0) {
                for (MysaveAlbumDB db : dbList) {
                    Album album = new Album(new ArrayList(), db.getAuthor(), db.getCover(), db.getDesc(), db.getId(), 0, db.getName(),null);
                    res.add(album);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

}
